package thread;


/**
 * 死锁的演示
 *
 * @author dingwen
 * @date 2021/09/05
 */
public class DeadLockDemo {
    public static void main(String[] args) {
        new Makeup(0,"lucy").start();
        new Makeup(1,"jack").start();
    }
}


/**
 * 口红
 *
 * @author dingwen
 * @date 2021/09/05
 */
class Lipstick{

}


/**
 * 镜子
 *
 * @author dingwen
 * @date 2021/09/05
 */
class Mirror{

}

class Makeup extends Thread{
    /**
     * 口红
     */
    public static Lipstick lipstick = new Lipstick();
    /**
     * 镜子
     */
    public static Mirror mirror = new Mirror();

    /**
     * 名字
     */
    private String name;
    /**
     * 类型
     */
    private int type;


    public Makeup(int type,String name){
        this.type = type;
        this.name = name;
    }
    @Override
    public void run() {
        try{
            makeup();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void makeup() throws InterruptedException {
        if (type == 0){
            synchronized (lipstick){
                System.out.println(name+"获得lipstick的锁");
                Thread.sleep(1000);
                synchronized (mirror){
                    System.out.println(name+"获得镜子的锁");
                }
            }
        }else{
            synchronized (mirror){
                System.out.println(name+"获的mirror的锁");
                Thread.sleep(1000);
                synchronized (lipstick){
                    System.out.println(name+"获得lipstick的锁");
                }

            }
        }
    }
}
